<?php
namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class UserInRoleController extends AbstractActionController{
	
	protected $userInRoleTable;
	
	public function getUserInRoleTable()
	{
		if(!$this->userInRoleTable)
		{
			$this->userInRoleTable = $this->getServiceLocator()->get('User\Model\UserInRoleTable');
		}
		
		return $this->userInRoleTable;
	}
	
	public function indexAction()
	{
		$result = $this->getUserInRoleTable()->fetchAll();
		return array('userinrole' => $result);
	}
	
	public function updateAction()
	{
		$form = $this->getServiceLocator()->get('User\Form\UserInRoleForm');
		
		if($this->request->isPost())
		{
			$postData = $this->params()->fromPost();
			
			$this->getUserInRoleTable()->save(array('uid' => $postData['uid'], 'rid' => $postData['rid']));
			
			$this->redirect()->toRoute('user/process', array('controller' => 'userinrole', 'action' => 'index'));
		}
		
		$uid = (int)$this->params()->fromQuery('id');
		
		$result = $this->getUserInRoleTable()->getById($uid);
		
		$form->populateValues($result);
		
		return array('form' => $form);
	}
	
	public function resetAction()
	{
		$uid = (int)$this->params()->fromQuery('id');
		$this->getUserInRoleTable()->reset($uid);
		$this->redirect()->toRoute('user/process', array('controller' => 'userinrole', 'action' => 'index'));
	}
	
}

?>